/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.colegiatura.adapters;

//import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.cip.esymons.general.be.ConsejoDepartamental;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.cip.esymons.colegiatura.bc.ConsejoDelegate;
//import org.cip.esymons.general.util.ControladorAbstracto;


public class RutaCarpetaReporte {

    private static final String ruta = "C:\\reportes\\"; //ruta Windows
    private static final String rutaReporte = "/jasper/";
    private static final String rutaPDF = "/modulos/reportes/";  // ruta Linux
    private static final String rutaPDFpreInscripcion = "/modulos/colegiatura/documentosPreinscripcion/";

    ConsejoDepartamental consejo = new ConsejoDepartamental();

    public RutaCarpetaReporte() {
//        retornarConsejo();
    }

    public ConsejoDepartamental getConsejo() {
        return consejo;
    }

    public ConsejoDepartamental retornarConsejo() {
        ConsejoDelegate delegate = new ConsejoDelegate();
        try {
            consejo = delegate.buscarActivo();
        } catch (Exception e) {
        }
        return consejo;
    }

    public static String getRuta() {
        return ruta;
    }

    public static String getRutaReporte() {
        return rutaReporte;
    }

    public static String getRutaPDF() {
        return rutaPDF;
    }

    public static String getRutaPDFpreInscripcion() {
        return rutaPDFpreInscripcion;
    }

    public boolean generarPDF(String rutaPDF,JasperPrint print ) {
        try {
            //rutaPDF = "http://192.168.2.53:8080" + rutaPDF;
            OutputStream output = new FileOutputStream(new File(rutaPDF));
            //Generar PDF
            JasperExportManager.exportReportToPdfStream(print, output);
            output.close();
            //abrir cual tipo de documento
            File path = new File(rutaPDF);
            System.out.println("BIEN HASTA AQUI reporte pdf....");
            //Desktop.getDesktop().open(path);  //funciona para windows
            try {
                abrirPDF(rutaPDF);
            } catch (FileNotFoundException f) {
                System.out.println("ERROR1: " + f.getMessage());
            } catch (IOException i) {
                System.out.println("ERROR2: " + i.getMessage());
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void abrirPDF(String strPath) throws FileNotFoundException, IOException {
        File fichero = new File(strPath);
        FacesContext ctx = FacesContext.getCurrentInstance();
        FileInputStream fis = new FileInputStream(fichero);
        byte[] bytes = new byte[1000];
        int read = 0;

        if (!ctx.getResponseComplete()) {
           String fileName = fichero.getName();
           //String contentType = "application/vnd.ms-excel";
           String contentType = "application/pdf";
           HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();

           response.setContentType(contentType);

           response.setHeader("Content-Disposition",
                              "attachment;filename=\"" + fileName + "\"");

           ServletOutputStream out = response.getOutputStream();

           while ((read = fis.read(bytes)) != -1) {
                out.write(bytes, 0, read);
           }

           out.flush();
           out.close();
           System.out.println("\nDescargado\n");
           ctx.responseComplete();
        }
    }

    public String generarExcel(String nomExcel, HSSFWorkbook wb) {
        try {
            //Exportar a Excel
            FacesContext fcontext = FacesContext.getCurrentInstance();
            ExternalContext eContext = fcontext.getExternalContext();
            HttpServletResponse response = (HttpServletResponse) eContext.getResponse();
            response.setHeader("Content-Disposition", "inline; filename=" + nomExcel);
            response.setHeader("Cache-Control", "public");
            response.setContentType("application/vnd.ms-excel");
            OutputStream sout = response.getOutputStream();
            wb.write(sout);
            sout.close();
            fcontext.responseComplete();
        } catch (Exception e) {
            System.out.print("Error------" + e.toString());
            e.printStackTrace();
        }

        return "";
    }



    public static void error(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, facesMsg);
    }

    public static void warn(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Exito: ", msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, facesMsg);
    }

    public static void info(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Informacion: ", msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage("successInfo", facesMsg);
    }

}
